package org.easytouseit.rabbitmq.direct.consumer;

import com.rabbitmq.client.Channel;
import org.easytouseit.rabbitmq.direct.DirectRabbitMQConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 消费者
 *
 * @author guojm
 * @version V1.0
 * @date 2018-12-28 下午 15:18
 */
@Component
public class DirectMsgReceiver {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @RabbitListener(queues = DirectRabbitMQConfig.QUEUE_NAME, containerFactory="rabbitListenerContainerFactory")
    @RabbitHandler
    public void process(String content, Channel channel, Message message) {
        logger.info("消费者1接收处理队列:{}当中的消息:{}", DirectRabbitMQConfig.QUEUE_NAME,content);
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("消息即将再次返回队列处理...");
            try {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,true);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

}
